package com.nariano.webapp;

import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.ResourceModel;

import com.nariano.server.domain.entities.City;
import com.nariano.server.domain.entities.Location;
import com.nariano.server.domain.entities.User;
import com.nariano.server.services.cityservice.CityService;
import com.nariano.server.services.userservice.UserService;
import com.nariano.server.utils.spring.SpringFactory;
import com.nariano.webapp.util.DetachedSessionName;
import com.nariano.webapp.util.ParameterizedResourceModel;

/**
 * Homepage
 */
public class Index extends WebPage {

	private static final long serialVersionUID = 1L;
	
	private static final CityService cityService = SpringFactory.getBean("cityService", CityService.class);
	private static final UserService userService = SpringFactory.getBean("userService", UserService.class);
	

    /**
     * Constructor that is invoked when page is invoked without a session.
     * 
     */
    public Index() {
        this(null);
    }
    
    public Index(final User opponentFocused) {
		this(opponentFocused, null);
	}

    private Index(final User opponentFocused, final PageParameters parameters) {
        
        
        IModel welcome = new ParameterizedResourceModel("welcome", new DetachedSessionName());
        
        add(new Label("welcome",welcome) {

            @Override
            public boolean isVisible() {
                return NarianoSession.get().isAuthenticied() == true;
            }});
        
        add(new PlayerLoginPanel("loginform", this.getPageClass()) {
            @Override
            public boolean isVisible() {
                return NarianoSession.get().isAuthenticied() == false;
            }
        });
        add(new Link("worldmap"){
            
            @Override
            public boolean isVisible() {
                return NarianoSession.get().isAuthenticied() == true;
            }

            @Override
            public void onClick() {
                setResponsePage(WorldMapPage.class);
            }});
        
        add(new CityPanel("cityPanel", getCity()){
        	@Override
            public boolean isVisible() {
                return NarianoSession.get().isAuthenticied() == true;
            }
        });
            
        add(new Link("logout", new ResourceModel("logout")){
            
            @Override
            public boolean isVisible() {
                return NarianoSession.get().isAuthenticied() == true;
            }

            @Override
            public void onClick() {
                NarianoSession.get().setUser(null);
                setResponsePage(Index.class);
       }});
        
        add(new Link("editplayer", new ResourceModel("editplayer")){
            
            @Override
            public boolean isVisible() {
                return NarianoSession.get().isAuthenticied() == true;
            }

            @Override
            public void onClick() {
                setResponsePage(new EditPlayerPage());
       }});
        
        ParameterizedResourceModel playerCount = new ParameterizedResourceModel("playereCount", Integer.valueOf(userService.getNofUsers()) );
        add(new Label("nofPlayers", playerCount));
         
        add(new TopListPlayersPanel("topListPlayers", 100,NarianoSession.get().getUser()));
        
        add(new OpponentInteractionPanel(opponentFocused, NarianoSession.get().getUser(), "opponentPanel"){
        	@Override
            public boolean isVisible() {
                return opponentFocused != null;
            }
        });


    }

	

	private City getCity() {
		User user = NarianoSession.get().getUser();
		if (user==null) {
			return null;
		}
        List<City> cities = cityService.getCities(user);
        if (cities.isEmpty()) {
            return null;
        } else {
        	// Get first city
            City city = cities.iterator().next();
            return city;
        }
	}


    

}
